package commons;
import gui.commons.CellRenderer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class MediaList extends ArrayList<Media>{

	private static final long serialVersionUID = 1L;

	private ArrayList<Media> content;

	public MediaList(){
		this.content=new ArrayList<Media>();
	}

	public void addMedia(Media media){
		content.add(media);
	}

	public synchronized void delMedia(Media media){
		String path=media.getPath();
		System.out.println("rm de : "+path);
		ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "rm \""+ path+"\"");
		pb.redirectErrorStream(true);
		try {
			pb.start();
		} catch (IOException e) {
		}
		content.remove(media);
	}

	public ArrayList<Media> getArrayList() {
		return content;
	}

	public synchronized String getMediaListString(){
		String str="";
		Iterator<Media> itr=content.iterator();
		while(itr.hasNext()){
			str=str+itr.next().toString()+";";
		}
		return str;
	}

	public synchronized Media[] getMediaListTab(){
		Media[] table = new Media[content.size()];
		Integer i=0;
		Iterator<Media> itr=content.iterator();
		while(itr.hasNext()){
			table[i]=itr.next();
			i++;
		}
		return table;
	}

	public synchronized JTable getMediaListJTab(){
		Object[][] data = {};
		String title[]={"media_name","user_name","index"};
		JTable table = new JTable(new DefaultTableModel(data,title));
		int i = 0;
		Iterator<Media> itr=content.iterator();
		while(itr.hasNext()){
			Media media = itr.next();
			String mediaName;
			String userName;
			String index;
			if(media.getTitle().equals("")){
				mediaName = media.getMediaName();
				userName = media.getUser().getName();
				index = ""+i;
			}
			else{
				mediaName = media.getTitle();
				userName = media.getUser().getName();
				index = ""+i;
			}

			DefaultTableModel modele = (DefaultTableModel)table.getModel();
			Object[] obj = new Object[3]; 
			obj[0] = mediaName;
			obj[1] = userName;
			obj[2] = index;

			modele.addRow(obj);
			table.setModel(modele);
			table.repaint();
		}
		table.setDefaultRenderer(Object.class, new CellRenderer());
		table.setShowGrid(false);
		return table;
	}

	public synchronized Media getFirst(){
		if(content.isEmpty()){
			return null;
		}else{
			return content.get(0);
		}
	}

	public synchronized Media getNext(){
		if(content.size()>1){
			return content.get(1);
		}else{
			return null;
		}
	}

	public synchronized int getNumberOfMedia(){
		return content.size();
	}

	public void upMedia(Media media){
		int ind=content.indexOf(media);
		if(ind>0){
			content.remove(ind);
			content.add(ind-1,media);		
		}
	}

	public synchronized Media getMediaAtIndex(int i){
		return content.get(i);
	}

	public synchronized Media searchByString(String name){
		Iterator<Media> itr=content.iterator();
		while(itr.hasNext()){
			Media tmp = itr.next();
			if(tmp.getMediaName().equals(name)){
				return tmp;
			}
		}
		return null;
	}
	
	public synchronized Media searchByTitle(String title){
		Iterator<Media> itr=content.iterator();
		while(itr.hasNext()){
			Media tmp = itr.next();
			if(tmp.getTitle().equals(title)){
				return tmp;
			}
		}
		return null;
	}

	public String toString(){
		return this.getMediaListString();
	}


}